package com.ltt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ltt.annotation.NoAuth;
import com.ltt.common.UserThreadLocal;
import com.ltt.dao.pojo.Chat;
import com.ltt.dao.pojo.Group;
import com.ltt.dao.vo.Result;
import com.ltt.mapper.GroupMapper;
import com.ltt.service.ChatService;
import com.ltt.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/1 22:03
 */
@CrossOrigin
@Api(tags = "学习小组模块")
@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private GroupService groupService;
    @Autowired
    private ChatService chatService;

    @Autowired
    private GroupMapper groupMapper;

    @ApiOperation("根据id查询学习小组的数据")
    @PostMapping("/findById")
    public Result findAllHarvest() {
        List<Group> allGroups = groupService.findAllGroupById(UserThreadLocal.get().getOpenid());
        return Result.successWithData(allGroups);
    }

    @ApiOperation("根据id查询学习小组的数据")
    @PostMapping("/findByIds")
    @NoAuth
    public Result findAllHarvests(@RequestBody String openId) {
        JSONObject jsonObject =  JSON.parseObject(openId);
        System.out.println(jsonObject.getString("openId"));
        List<Group> allGroups = groupService.findAllGroupById(jsonObject.getString("openId"));
        System.out.println(allGroups);
        return Result.successWithData(allGroups);
    }

    @ApiOperation("根据")
    @PostMapping("/findByName")
    @NoAuth
    public Result findByName(@RequestBody String name) {
        JSONObject jsonObject =  JSON.parseObject(name);
        System.out.println(jsonObject.getString("findByName"));
        List<Group> allGroups = groupMapper.findByName(jsonObject.getString("findByName"));
        System.out.println(allGroups);
        return Result.successWithData(allGroups);
    }

    @ApiOperation("退出学习小组")
    @PostMapping("/exit")
    public Result exitGroup(@RequestBody String id) {
        boolean res = groupService.exitGroup(id, UserThreadLocal.get().getOpenid());
        if(res) {
            return Result.successWithoutData();
        } else {
            return Result.failed("退出失败");
        }
    }

    @ApiOperation("学习小组聊天记录")
    @PostMapping("/getChatList")
    public Result getChatList(@RequestBody String id) {
        System.out.println(id);
        List<Chat> chats = chatService.getChatListByGroupId(id);
        System.out.println(chats);
        return Result.successWithData(chats);
    }

    @ApiOperation("创建学习小组")
    @PostMapping("/insert")
    public Result insert(@RequestBody Group group){
        System.out.println(group);
        boolean res = groupService.insert(group, UserThreadLocal.get().getOpenid());
        return res ? Result.successWithoutData() : Result.failed("创建失败");
    }

    @ApiOperation("学习小组信息")
    @PostMapping("/getGroupItem")
    public Result findGroupById(@RequestBody String id) {
        Group group = groupService.findGroupById(id, UserThreadLocal.get().getOpenid());
        return Result.successWithData(group);
    }

    @ApiOperation("发送聊天记录")
    @PostMapping("/setChat")
    public Result setChat(@RequestBody Chat chat){
        chat.setInitiator(UserThreadLocal.get().getOpenid());
        System.out.println(chat);
        boolean res = chatService.setChat(chat);
        return res ? Result.successWithoutData() : Result.failed("发送失败");
    }

    @ApiOperation("修改学习小组信息")
    @PostMapping("/updateGroup")
    public Result updateGroupItem(@RequestBody Group group) {
        boolean res = groupService.updateGroupItem(group);
        return res? Result.successWithData(res) : Result.failed("修改失败");
    }

    @ApiOperation("学习小组查询全部")
    @PostMapping("/findAll")
    public Result findAllGroup() {
        List<Group> allGroups = groupService.findAllGroup(UserThreadLocal.get().getOpenid());
        return Result.successWithData(allGroups);
    }

    @ApiOperation("加入学习小组")
    @PostMapping("/add")
    public Result addGroup(int groupId, int isA){
        System.out.println(groupId + "" + " ," + isA);
        boolean res = groupService.addGroup(groupId,UserThreadLocal.get().getOpenid(), isA);
        return res? Result.successWithData(res) : Result.failed("添加失败");
    }
}
